package com.tpo.jnotes.client.lib.component.optionpane;

import static com.tpo.jnotes.base.colors.schema.BCSSManager.getCurrentCSS;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.MatteBorder;

import com.tpo.jnotes.base.colors.BColors;
import com.tpo.jnotes.base.controls.BTransparentPanel;
import com.tpo.jnotes.base.resource.BResConstants;
import com.tpo.jnotes.base.resource.BResource;
import com.tpo.jnotes.client.lib.component.buttons.IconButton;
import com.tpo.jnotes.client.lib.constant.OptionPaneResults;
import com.tpo.jnotes.client.lib.util.StringsUtils;

public class NCustomOptionPane extends JDialog implements ActionListener
{
    private final JTextField valueTextEdit;
    private final JButton okButton;
    private final JButton cancelButton;
    private final String initalValue;
    private OptionPaneResults answer;

    public NCustomOptionPane( Component parent, String title, String initialValue )
    {
        setResizable( false );
        setUndecorated( true );
        setModalityType( ModalityType.APPLICATION_MODAL );
        setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
        setAlwaysOnTop( true );
        setModal( true );
        getContentPane().setLayout( new BorderLayout( 0, 0 ) );

        BTransparentPanel topPanel = new BTransparentPanel( new BorderLayout( 0, 0 ) );
        topPanel.setBorder( new MatteBorder( 0, 1, 1, 0, getCurrentCSS().getBorderColor() ) );
        getContentPane().add( topPanel, BorderLayout.CENTER );

        valueTextEdit = new JTextField();
        topPanel.add( valueTextEdit, BorderLayout.CENTER );
        valueTextEdit.setBorder( null );
        valueTextEdit.setForeground( getCurrentCSS().getPanelForeground() );
        valueTextEdit.setBackground( getCurrentCSS().getPanelBackground() );
        valueTextEdit.setHorizontalAlignment( SwingConstants.CENTER );
        valueTextEdit.setColumns( 10 );
        valueTextEdit.setText( initialValue );
        valueTextEdit.setFont( getCurrentCSS().getPanelFont() );

        JLabel titleLabel = new JLabel( title, SwingConstants.CENTER );
        titleLabel.setOpaque( true );
        topPanel.add( titleLabel, BorderLayout.NORTH );
        titleLabel.setBackground( getCurrentCSS().getTitleBackground() );
        titleLabel.setForeground( getCurrentCSS().getTitleForeground() );
        titleLabel.setFont( getCurrentCSS().getTitleFont() );
        titleLabel.setFont( getCurrentCSS().getPanelFont() );

        JPanel bottomPanel = new JPanel( new BorderLayout( 0, 0 ) );
        bottomPanel.setBackground( getCurrentCSS().getPanelBackground() );
        topPanel.add( bottomPanel, BorderLayout.EAST );
        bottomPanel.setBorder( new MatteBorder( 0, 0, 0, 1, getCurrentCSS().getBorderColor() ) );

        okButton = new IconButton( BResource.getIcon( BResConstants.OK_IMG ) );
        okButton.setBackground( BColors.WHITE_D );
        okButton.setPreferredSize( new Dimension( 24, 24 ) );
        okButton.addActionListener( this );

        cancelButton = new IconButton( BResource.getIcon( BResConstants.CANCEL_IMG ) );
        cancelButton.setBackground( BColors.WHITE_D );
        cancelButton.setPreferredSize( new Dimension( 24, 24 ) );
        cancelButton.addActionListener( this );
        bottomPanel.setLayout( new BorderLayout( 0, 0 ) );

        bottomPanel.add( okButton, BorderLayout.NORTH );
        bottomPanel.add( cancelButton, BorderLayout.SOUTH );

        this.initalValue = initialValue;
        this.answer = OptionPaneResults.CANCEL;
    }

    public static String showInputDialog( Component parent, int messageType, String title, String initialValue )
    {
        NCustomOptionPane optPane = new NCustomOptionPane( parent, title, initialValue );
        optPane.pack();
        optPane.setLocationRelativeTo( parent );
        optPane.setVisible( true );
        return optPane.getValue();
    }

    private String getValue()
    {
        if( (answer == OptionPaneResults.CANCEL)
            || StringsUtils.equals( initalValue, valueTextEdit.getText() ) )
            return null;
        else
            return valueTextEdit.getText();
    }

    @Override
    public void actionPerformed( ActionEvent e )
    {
        if( e.getSource() == okButton )
            answer = OptionPaneResults.OK;
        else
            answer = OptionPaneResults.CANCEL;
        setVisible( false );
    }

}
